package com.atguigu.common.util;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class RegexValidateUtils {

    public static boolean flag = false;
    static String regex = "";

    public static boolean check(String str, String regex) {
        try {
            Pattern pattern = Pattern.compile(regex);
            log.info("pattern:{}", pattern);
            Matcher matcher = pattern.matcher(str);
            flag = matcher.matches();
        }catch (Exception e){
            flag = false;
        }
        return flag;
    }

    /**
     * 验证邮箱
     * @param email
     * @return
     */
    public static boolean checkEmail(String email){
            String regex = "^\\w+[-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
            return check(email, regex);
    }

    /**
     * 校验手机号
     * @param mobile
     * @return
     */
    public static boolean checkMobile(String mobile){
        String regex = "^((13[0-9])|(14[0-9])|(15([0-9]))|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
        return check(mobile, regex);
    }
}
